//
//  LTClassDetailViewController.m
//  LearnTeacher
//
//  Created by zyl_macbookpro on 2024/4/10.
//

#import "LTClassDetailViewController.h"
#import "LTClassDetaiTableViewCell.h"
#import "LTClassAPIManager.h"
#import "LTClassDetailModel.h"
#import "LTCLassDetailGroupModel.h"
#import "LTCLassDetailStudentModel.h"
#import "CustomActionSheetAlertView.h"
@interface LTClassDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)NSDictionary *classDic;

@property(nonatomic ,strong)LTClassDetailModel *model;

@end

@implementation LTClassDetailViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:LTClearColor] backgroundColor:LTClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(0, 0, 0, 1),NSFontAttributeName:MFontWithSize(17)} enableScrollEdgeAppearance:NO];
    [self loadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.classDic[@"grade_name"];
    [self.view addSubview:self.tableView];
    [self createRightBarButtonItem];
}
-(void)loadData
{
    [LTClassAPIManager getClassInfoWithParam:@{@"id":self.classDic[@"id"]} result:^(NSDictionary * _Nonnull dic) {
        if(dic[@"data"])
        {
            self.model = [LTClassDetailModel modelWithDictionary:dic[@"data"]];
            [self.tableView reloadData];
            self.title = self.model.grade_name;
        }
    }];
}
- (void)resetPwdWithModel:(LTCLassDetailStudentModel *)studentModel
{
    [LTClassAPIManager resetPwdWithParam:@{@"id":studentModel.student_id} result:^(NSDictionary * _Nonnull dic) {
        [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"重置成功 %@ 密码：%@",studentModel.nickname,dic[@"data"][@"password"]]];//
    }];
}
- (void)createRightBarButtonItem
{
    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 44)];
    button.titleLabel.font =RFontWithSize(16);
    [button setTitle:@"修改" forState:UIControlStateNormal];
    [button setTitleColor:rgba(41, 121, 255, 1) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(changeGroup) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItems =@[item];
}
-(void)changeGroup
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"LTChangeClassInfoVC"];
    vc.paraObject = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)doGroupClick:(UIButton *)button
{
    LTCLassDetailGroupModel *groupModel = self.model.group[button.tag - 1];
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"LTCreateGroupViewController"];
    vc.paraObject = groupModel;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTClassDetaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTClassDetaiTableViewCell"];
    LTCLassDetailGroupModel *groupModel = self.model.group[indexPath.section];
    groupModel.class_id = self.classDic[@"id"];
    LTCLassDetailStudentModel *studentModel = groupModel.student[indexPath.row];
    studentModel.class_id = self.classDic[@"id"];
    cell.model = studentModel;
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  
    return self.model.group.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    LTCLassDetailGroupModel *groupModel = self.model.group[section];
    return groupModel.student.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 37;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LTCLassDetailGroupModel *groupModel = self.model.group[section];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LTScreenWidth, 37)];
        
        UILabel * subLabel = [[UILabel alloc] initWithFrame:CGRectMake(24, 15, LTScreenWidth - 84, 18)];
        subLabel.font = RFontWithSize(13);
        subLabel.text = [NSString stringWithFormat:@"%@ (%@)",groupModel.name,groupModel.studentNum];
        [subLabel setTextAlignment:NSTextAlignmentLeft];
        subLabel.textColor = rgba(102, 102, 102, 1);
    UIButton *clickBtn =  ({
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font =RFontWithSize(12);
        [button setTitle:@"分组" forState:UIControlStateNormal];
        [button setTitleColor:rgba(153, 153, 153, 1) forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ic_right_arrow_gray"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(doGroupClick:) forControlEvents:UIControlEventTouchUpInside];
        button.adjustsImageWhenHighlighted = NO;
        [button layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:0];
        button;
        
    });
    clickBtn.tag = section + 1;
    [clickBtn setFrame:CGRectMake(LTScreenWidth - 54, 12, 36, 25)];
    [headerView addSubview:clickBtn];
    [headerView addSubview:subLabel];
    return headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CustomActionSheetAlertView *alertView = [[CustomActionSheetAlertView alloc] init];
    alertView.cancelTitleColor = @"#000000";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    LTCLassDetailGroupModel *groupModel = self.model.group[indexPath.section];
    groupModel.class_id = self.classDic[@"id"];
    LTCLassDetailStudentModel *studentModel = groupModel.student[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@（%@）",studentModel.username,studentModel.username];
    [dic setObject:str forKey:@"title"];
    [dic setObject:@"#727A8E" forKey:@"titleColor"];
    [dic setObject:@"14" forKey:@"font"];
    [alertView addActionWithTitle:dic Action:^{
       
    }];
   
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic2 setObject:@"复制账号" forKey:@"title"];
    [dic2 setObject:@"#000000" forKey:@"titleColor"];
    [alertView addActionWithTitle:dic2 Action:^{
        [UIPasteboard generalPasteboard].string = studentModel.username;
        [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"复制 %@ 成功",str]];//
    }];
    
    NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic3 setObject:@"重置密码" forKey:@"title"];
    [dic3 setObject:@"#000000" forKey:@"titleColor"];
    [alertView addActionWithTitle:dic3 Action:^{
        [self resetPwdWithModel:studentModel];
    }];
   
    [alertView showInView:self.navigationController.tabBarController.view];
  
    
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LTTopBarHeight, LTScreenWidth, LTScreenHeight - LTTopBarHeight) style:UITableViewStyleGrouped];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTViewBgGray];
        [_tableView setSeparatorColor:LTClearColor];
        
        [_tableView registerNib:[UINib nibWithNibName:@"LTClassDetaiTableViewCell" bundle:nil] forCellReuseIdentifier:@"LTClassDetaiTableViewCell"];
      
    }
    
    return _tableView;
}
- (void)setParaObject:(NSObject *)paraObject
{
    self.classDic = (NSDictionary *)paraObject;
}

@end
